package com.atguitu.cm.ui;

import com.atguitu.cm.bean.Customer;
import com.atguitu.cm.service.CustomerList;
import com.atguitu.cm.util.CMUtility;

/**
 * CustomerView为主模块
 * 负责菜单的显示和处理用户操作
 * 
 * @author Unicorn
 */
public class CustomerView {
    private CustomerList customers = new CustomerList(10);

    //主方法
    public static void main(String[] args) {
        CustomerView view = new CustomerView();
        view.enterMainMenu();
    }

    /**
     * 进入主菜单,响应客户的操作
     */
    public void enterMainMenu() {
        boolean loopFlag = true;
        do {
            System.out.println("\n\n--------------------客户信息管理软件----------------------");
            System.out.println("\n\t\t    1 添加客户");
            System.out.println("\t\t    2 修改客户");
            System.out.println("\t\t    3 删除客户");
            System.out.println("\t\t    4 客户列表");
            System.out.println("\t\t    5 退      出");
            System.out.print("\n\t\t  请选择(1~5)：");
            char key = CMUtility.readMenuSelection();
            switch (key) {
            case '1':
                addNewCustomer();
                break;
            case '2':
                modifyCustomer();
                break;
            case '3':
                deleteCustomer();
                break;
            case '4':
                listAllCustomers();
                break;
            case '5':
                System.out.println("\n---------------------退出成功--------------------------");
                return;
            }

        } while (loopFlag);
    }


    /**
     * 添加客户
     */
    private void addNewCustomer() {
        System.out.println("\n---------------------添加客户--------------------------");
        // 1.做准备
        System.out.println("请输入您的姓名:");
        String name = CMUtility.readString(4);
        System.out.println("请输入您性别:");
        char gender = CMUtility.readChar();
        System.out.println("请输入您的电话:");
        String phone = CMUtility.readString(11);
        System.out.println("请输入您的邮箱:");
        String email = CMUtility.readString(14);
        System.out.println("请输入您的年龄:");
        int age = CMUtility.readInt();

        // 2.将数据封装到对象中
        Customer cus = new Customer(name, gender, age, phone, email);

        // 3.插入客户对象到数组内
        boolean flag = customers.addCustomer(cus);

        // 4.判断是否插入成功
        if (flag) {
            System.out.println("插入成功");
        } else {
            System.out.println("记录已满,无法插入");
        }

    }


    /**
     * 修改客户
     */
    private void modifyCustomer() {
        System.out.println("\n---------------------修改客户--------------------------");
        // 1.校验指定客户是否在数组内
        Customer cust = null;
        int index = -1;
        while (true) {
            System.out.println("请输入修改客户的编号(-1退出):");
            index = CMUtility.readInt();
            // 判断编号是否为-1
            if (index == -1) {
                return;
            }
            // 去数组中查找
            cust = customers.getCustomer(index - 1);
            if (cust == null) {
                System.out.println("没有找到指定的客户");
            } else {
                break;
            }

        }

        // 2.指定客户存在继续修改
        System.out.print("姓名(" + cust.getName() + ")");
        String name = CMUtility.readString(4, cust.getName());
        System.out.print("性别(" + cust.getGender() + ")");
        char gender = CMUtility.readChar(cust.getGender());
        System.out.print("年龄(" + cust.getAge() + ")");
        int age = CMUtility.readInt(cust.getAge());
        System.out.print("电话(" + cust.getPhone() + ")");
        String phone = CMUtility.readString(11, cust.getPhone());
        System.out.print("邮箱(" + cust.getEmail() + ")");
        String email = CMUtility.readString(14, cust.getEmail());
        // 将数据封装到对象内
        cust = new Customer(name, gender, age, phone, email);

        // 执行修改
        boolean flag = customers.replaceCustomer(index - 1, cust);
        // 3.判断是否修改成功
        if (flag) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }

    }

    /**
     * 删除客户的操作
     */
    private void deleteCustomer() {
        System.out.println("\n---------------------删除客户--------------------------");
        // 1.判断数组内是否含有指定的对象
        int index = -1;
        Customer cust = null;
        while (true) {
            System.out.println("请选择删除客户的编号(-1退出):");
            index = CMUtility.readInt();

            if (index == -1) {
                return;
            }
            // 去数组内查找
            cust = customers.getCustomer(index - 1);
            if (cust == null) {
                System.out.println("查无此人");
            } else {
                break;
            }
        }

        // 2.进行删除操作
        System.out.println("确认是否删除(Y/N):");
        char choose = CMUtility.readConfirmSelection();

        if (choose == 'Y') {
            boolean flag = customers.deleteCustomer(index - 1);
            if (flag) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        } else {
            return;
        }
    }


    /**
     * 展示所有的客户信息
     */
    private void listAllCustomers() {
        // 1.当数组内没有元素时
        Customer[] cust = customers.getAllCustomers();
        if (cust.length == 0) {
            System.out.println("系统内没有客户信息");
        } else {
            // 2.当数组内含有元素时,进行数据展示
            System.out.println("姓名\t性别\t年龄\t电话\t\t邮箱");

            for (Customer c : cust) {
                System.out.println(c.getName() + "\t" + c.getGender() + "\t" + c.getAge() + "\t" + c.getPhone()
                        + "\t" + c.getEmail());
            }

        }

    }

}
